/*
 * 如果是被包含的文件，尽量选择为.hpp而非.cpp，否则可能陷入重复包含
 * */
#include "08_MyTask.hpp"
#include <iostream>

#define info_msg(msg) do { \
    printf("%s | %d | %s | %s \n", __FILE__, __LINE__, __FUNCTION__, msg); \
} while(0)

namespace xy
{
// 声明一个外部变量来访问服务器实例
xy::TcpServer* g_server = nullptr;

// 处理ctrl + c信号，优雅退出
void signal_handler(int signum)
{
    info_msg("Server shutdown...");
    if(g_server)
    {
        info_msg("if->stop");
        g_server->stop();
    }
    info_msg("not into if");
    //exit(0);      // 移出exit(0)调用，因为在TcpServer::stop()中已经准备关闭代码
}

// 定义回调函数
void on_connection(const TCPconnection::TCPconnectionPtr& conn)
{
    std::cout << "New connection established: " << conn->tostring() << "\n";
}

void on_message(const TCPconnection::TCPconnectionPtr& conn, const std::string &msg)
{
    std::cout << "Received message: " << conn->tostring() << msg << "\n\n";
}

void on_close(const TCPconnection::TCPconnectionPtr& conn)
{
    std::cout << "Connection closed: " << conn->tostring() << "\n";
}

}

